<?php
if( !defined('CORE') ) exit('Request Error!');
class ctl_producecode extends common
{
    public function __construct()
    {
        $this->_dbfield=array(

            'mainKey'=>'tpl_id',

            'allTableField'=>array('tpl_id'=>'编号','tpl_fz'=>'团长ID','tpl_name'=>'VIP激活码(点我复制激活码)','tpl_struct'=>'是否使用(0未使用/1已使用)','tpl_fxid'=>'分销ID','tpl_uid'=>'使用者ID','tpl_addtime'=>'添加时间'),

            'addTableField'=>array('tpl_name','tpl_struct'),



            'editTableField'=>array('tpl_name','tpl_fz','tpl_struct','tpl_fxid','tpl_uid','tpl_addtime'),



            'listTableField'=>array('tpl_id','tpl_fz','tpl_name','tpl_struct','tpl_fxid','tpl_uid','tpl_addtime'),



            'batchUpdateTableField'=>array('tpl_name'),

            'batchDeleteTableField'=>array(

                'mainKey'=>'tpl_id',

            ),

            'tpl_id'=>array(

                'element'=>array('e_name'=>'input','e_type'=>'text'),

                'search'=>'1',

            ),

            'tpl_name'=>array(

                'element'=>array('e_name'=>'input','e_type'=>'text'),

                'search'=>'1',

                'search_extend'=>'like',

            ),

            'tpl_addtime'=>array(

                'element'=>array('e_name'=>'input','e_type'=>'text'),

            ),

        );
        parent::__construct();
        $this->table = 'code_template';

        $this->_allowAction = array(

            'add'=>array('title'=>'添加','type'=>'dialog','width'=>'700','height'=>'600'),

            'edit'=>array('title'=>'编辑','type'=>'href',),

            //'delete'=>array('title'=>'删除','type'=>'href',),



        );
        tpl::assign('_allowAction',$this->_allowAction);
        tpl::assign('_submit_validate',$this->_submit_validate);
        tpl::assign('_dbfield',$this->_dbfield);
    }
    public function get_code(){
        $p_code = $this->make_invite_code();
        $result = db::get_one("select tpl_name from code_template where tpl_name ='".$p_code."'");
        if($result){
             $this->get_code();
        }else{
            $total_data = array(
                'n' => $p_code
            );
            $data['tpl_name'] = $p_code;
            $data['tpl_fz'] = req::item('tpl_fz');
            $data['tpl_struct'] = 0;
            $data['tpl_addtime'] = date('Y-m-d');
            db::insert('code_template',$data);
            if(db::insert_id ()){
                echo json_encode($total_data);
            }else{
                echo '邀请码未能入库！';
            }

        }

    }
    public function make_invite_code()
    {
        $code = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
        $rand = $code[rand(0, 25)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        for (
            $a = md5($rand, true),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 6;
            $g = ord($a[$f]), // ord（）函数获取首字母的 的 ASCII值
            $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],//按位亦或，按位与。
            $f++
        ) ;
        return $d;
    }
}